package com.example.worldcupsever.controller;

import com.example.worldcupsever.bean.Shou;
import com.example.worldcupsever.dao.ShouDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/shou")
public class ShouController {
    @Autowired
    ShouDao shouDao;

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Shou> shouList = shouDao.findAll();
        if (shouList == null) {
            map.put("success", false);
            map.put("msg", "失败");
        } else {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list", shouList);
        }
        return map;
    }
    //add
    @RequestMapping("/add")
    @ResponseBody
    public Map<String ,Object>add(@RequestBody Shou shou){
        Map<String,Object> map=new HashMap<>();
        Shou shou1 =shouDao.saveAndFlush(shou);
        if (shou1!=null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", shou1);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    //findone
    @RequestMapping("/findone/{id}")
    @ResponseBody
    public Map<String, Object> findoneById(@PathVariable Integer id) {
        Map<String, Object> map = new HashMap<>();
        Optional<Shou> shou1 = shouDao.findById(id);
        if (shou1!= null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", shou1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }



}
